#!/bin/bash
# +----- Tip: use jenkins can help you deploy easily -----+
# cd ${project.path}/common && mvn clean install
# cd ../api && mvn clean package -Dmaven.test.skip=true
# cd ../auth && mvn clean package -Dmaven.test.skip=true
# cd ../gateway && mvn clean package -Dmaven.test.skip=true
# +-------------------------------------------------------+
# please make sure the target package-jar and this script are placed in same directory before run this script.
thisDir=$(pwd)
# api
echo "【api】"
apiPort1=9096
sp_pid1=`ps -ef | grep api-1.0.jar | grep -v grep | grep -w ${USER} | awk '{print $2}'`
if [ -z "$sp_pid1" ];
then
 echo "[ api not running this time, now starting api-1.0.jar ... ]"
 nohup java -Xmx128m -jar -Djasypt.encryptor.password=myedu -Djasypt.encryptor.algorithm=PBEWithMD5AndDES ./api/api-1.0.jar >${thisDir}/logs/api.log --server.port=${apiPort1} --spring.datasource.url=jdbc:oracle:thin:@localhost:1521:xe --spring.redis.cluster.nodes=172.30.191.27:7000,172.30.191.27:7001,172.30.191.27:7002,172.30.191.27:7003,172.30.191.27:7004,172.30.191.27:7005 2>&1 &
  if [ $? -eq 0 ]; then
    echo "[ start successfully! api are running ]"
  else
    echo "[ start failed! api will try restart later ]"
  fi
else
 echo "[ api already running, found PID: $sp_pid1 ]"
fi
sleep 5
# check run status of api. nothing to do if it is running, else rerun and recheck.
index=0
while [ 1 > 0 ]
do
    sp_pid=`netstat -ntlp| grep ${apiPort1} | awk '{print $7}'| awk -F '/' '{print $1}'`
    if [ -z "$sp_pid" ]
    then
        # if index > 3
        if [ $index -gt 3 ]; then
          echo "Api service attempt to restart three times but still failures!" | mail -s "Fail to start Api" 1824301866@qq.com
          # exit current script
          exit;
        fi
        sleep 3
        nohup java -Xmx128m -jar -Djasypt.encryptor.password=myedu -Djasypt.encryptor.algorithm=PBEWithMD5AndDES ./api/api-1.0.jar >${thisDir}/logs/api.log --server.port=${apiPort1} --spring.datasource.url=jdbc:oracle:thin:@localhost:1521:xe --spring.redis.cluster.nodes=172.30.191.27:7000,172.30.191.27:7001,172.30.191.27:7002,172.30.191.27:7003,172.30.191.27:7004,172.30.191.27:7005 2>&1 &
        if [ $? -eq 0 ]; then
          break
        else
          echo "第$index次重启api service失败"
        fi
        let index++
        continue
    else
        break
    fi
done
# gateway
echo "【gateway】"
apiPort2=8080
sp_pid2=`ps -ef | grep gateway-1.0.jar | grep -v grep | grep -w ${USER} | awk '{print $2}'`
if [ -z "$sp_pid2" ];
then
 echo "[ gateway not running this time, now starting gateway-1.0.jar ... ]"
 nohup java -Xmx128m -jar ./gateway/gateway-1.0.jar >${thisDir}/logs/gateway.log --server.port=${apiPort2} 2>&1 &
 echo "[ start successfully! gateway are running ]"
else
 echo "[ gateway already running, found PID: $sp_pid2 ]"
fi
# auth
echo "【auth】"
apiPort3=8901
sp_pid3=`ps -ef | grep auth-1.0.jar | grep -v grep | grep -w ${USER} | awk '{print $2}'`
if [ -z "$sp_pid3" ];
then
 echo "[ auth not running this time, now starting auth-1.0.jar ... ]"
 nohup java -Xmx128m -jar ./auth/auth-1.0.jar >${thisDir}/logs/auth.log --server.port=${apiPort3} --spring.datasource.url=jdbc:oracle:thin:@localhost:1521:xe 2>&1 &
 echo "[ start successfully! auth are running ]"
else
 echo "[ auth already running, found PID: $sp_pid3 ]"
fi
# add parameter sleep to prevent the process killed by Jenkins after building #